Imports System
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Data.Common
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Practices.EnterpriseLibrary.Caching.Expirations
Imports ViewStateEliminator_VB.ViewStateEliminator.Configuration
Imports ViewStateEliminator_VB.ViewStateEliminator.Data



Namespace ViewStateEliminator.Data
    ''' <summary>
    ''' Interface that must be implemented to create a Viewstate Storage Medium
    ''' </summary>
    ''' <remarks></remarks>
    Public Interface IViewstateStorage
        ''' <summary>
        ''' Adds a key-value pair to the storage medium.
        ''' </summary>
        ''' <param name="viewstateItem"></param>
        ''' <remarks></remarks>
        Sub Add(ByVal viewstateItem As KeyValuePair(Of Object, String))
        ''' <summary>
        ''' Gets a key-value pair by a key
        ''' </summary>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function [Get](ByVal key As Object) As KeyValuePair(Of Object, String)
        ''' <summary>
        ''' Remove a key-value pair by key
        ''' </summary>
        ''' <param name="key"></param>
        ''' <remarks></remarks>
        Sub Remove(ByVal key As Object)
    End Interface
    Public Module ViewstateStorageFactory
        Public Function GetStorageMedium() As IViewstateStorage
            Dim config As ViewStateConfiguration = CType(System.Configuration.ConfigurationManager.GetSection("ViewStateConfiguration"), ViewStateConfiguration)
            Dim _retval As IViewstateStorage = GetStorageMedium(config.EliminatorMode)
            If IsNothing(_retval) Then
                Return New ViewStateSessionStorage()
            Else
                Return _retval
            End If
        End Function
        Function GetStorageMedium(ByVal type As String) As IViewstateStorage
            Dim Itype As System.Type = System.Type.GetType(type)
            Return CType(Activator.CreateInstance(Itype), IViewstateStorage)
        End Function
        Function GetUserUniqueID() As IUserUniqueID
            Dim config As ViewStateConfiguration = CType(System.Configuration.ConfigurationManager.GetSection("ViewStateConfiguration"), ViewStateConfiguration)

            Dim Itype As System.Type = System.Type.GetType(config.UniqueIdSource)
            Dim _retval As IUserUniqueID = CType(Activator.CreateInstance(Itype), IUserUniqueID)
            Return _retval
        End Function
    End Module
End Namespace
